<%@ page language="java" import="java.util.*" import="java.sql.*" import = "orm.*"  pageEncoding="utf-8" errorPage="errorPage.jsp"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% 	String courseOpt = ""; 
	String knowledgesOpt = "";
	String questionsOpt = "";
	List courses = (List)request.getAttribute("courseList");
	Map knowledgeMap = (Map)session.getAttribute("knowledgeMap");
	Map questionMap = (Map)session.getAttribute("questionMap");
	
	if (courses == null || courses.size() == 0){
		courseOpt = "<option value=''>您没有课程！</option>";
	}
	else {
		for(int i = 0; i < courses.size(); i++){
			Course course = (Course)courses.get(i);
			String name = course.getCoursename();
			if (i == 0){
				courseOpt += "<option value = '" + name + "' selected='selected' >" + name + "</option>";
				Map knowledges = (Map)knowledgeMap.get(name);
				if (knowledges != null && knowledges.size() > 0){
					Set kIds = knowledges.keySet();
					Iterator it = kIds.iterator();
					for (int j = 1; it.hasNext();){
						Long o = (Long)it.next();
						Knowledge k = (Knowledge)knowledges.get(o);
						if (j == 1){
							knowledgesOpt += "<option selected = 'selected' value = '" + o + "'>" + k.getKname() + "</option>";
							Set questions = (Set)questionMap.get(o);
							if (questions != null && questions.size() > 0){
								int qcnt = 1;
								for (Iterator it2 = questions.iterator(); it2.hasNext();){
									Question q = (Question)it2.next();
									Long qid = new Long(q.getQid());
									if (qcnt == 1){
										questionsOpt += "<option selected = 'selected' value = '" + qid + "'>" + q.getQtitle() + "</option>";
										qcnt++;
										continue;
									}
									questionsOpt += "<option value = '" + qid + "'>" + q.getQtitle() + "</option>";
								}
								session.setAttribute("questions", questions);
							}
							else{
								questionsOpt = "<option value=''>该知识点下没有题目！</option>";
								session.removeAttribute("questions");
							}
							j++;
							continue;
						}
						knowledgesOpt += "<option value = '" + o + "'>" + k.getKname() + "</option>";
					}
				}
				else{
					knowledgesOpt = "<option value=''>该课程下没有知识点！</option>";
				}
				continue;
			}
			courseOpt += "<option value = '" + name + "'>" + name + "</option>";
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href = "<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>题库管理</title>
<link href="css/tStyles.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="js/AjaxRequest.js">
</script>
<script type="text/javascript">
	<%if (request.getAttribute("success") != null){%>alert("<%=request.getAttribute("success")%>");<%request.removeAttribute("success");}
	else if (request.getAttribute("error") != null){%>alert("<%=request.getAttribute("error")%>");<%request.removeAttribute("error");}%>
	function showKnowledges(){
		c = this.course.value;
		var loader=new net.AjaxRequest("ajax.do?action=getKnowledges&c=" + c,show,onerror,"GET");
	}
	function display(){
		var loader3=new net.AjaxRequest("ajax.do?action=questions_display",displayButton,onerror,"GET");
	}
	function showQuestions(){
		k = this.know.value;
		var loader2=new net.AjaxRequest("ajax.do?action=getQuestions&k=" + k,showQ,onerror,"GET");
	}
	function searchQuestions(){
		var searchKey = document.getElementById("searchQ").value;
		var loader4=new net.AjaxRequest("ajax.do?action=searchQuestions&searchKey=" + encodeURIComponent(searchKey),showResult,onerror,"GET");
	}
	function onerror(){
	}
	function showResult(){
		document.getElementById("st_2").innerHTML=this.req.responseText;
	}
	function show(){
		document.getElementById("know").innerHTML=this.req.responseText;
		showQuestions();
	}
	function showQ(){
		document.getElementById("st_2").innerHTML=this.req.responseText;
		display();
	}
	function displayButton(){
		var str = this.req.responseText;
		if (str.indexOf("display")!= -1){
			document.getElementById("searchQ").style.visibility = 'visible';
			document.getElementById("searchQBtn").style.visibility = 'visible';
			document.getElementById("modifyQ").style.visibility = 'visible';
			document.getElementById("deleteQ").style.visibility = 'visible';
		}
		else{
			document.getElementById("searchQ").style.visibility = 'hidden';
			document.getElementById("searchQBtn").style.visibility = 'hidden';
			document.getElementById("modifyQ").style.visibility = 'hidden';
			document.getElementById("deleteQ").style.visibility = 'hidden';
		}
	}
	function operateQuestions(str){
		if (str.indexOf("addQ") != -1){
			window.location.href = "manageQuestions.do?action=questions&method=add&kid=" 
					+ this.know.value;
		}
		else if (str.indexOf("modifyQ") != -1){
			window.location.href = "manageQuestions.do?action=questions&method=view&kid=" 
				+ this.know.value + "&qid=" + this.st_2.value;
		}
		else if (str.indexOf("deleteQ") != -1){
			var opt = confirm("本次删除不可恢复，是否删除该题目?");
			if (opt == true){
				window.location.href = "manageQuestions.do?action=deleteQuestion&qid=" + this.st_2.value;
			}
		}
	}
</script>
</head>
<% 	User tea  =(User) session.getAttribute("Teacher");  
	if (tea == null){
		response.sendRedirect(path + "/index.jsp");
	}
%> 
<body>
	<!-- CONTAINER START -->
<div id = "container">
	<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
            	<html:link page="/teacher/teacher.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
                <html:link page="/teacher/notice.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
                <html:link action="manageQuestions.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;题库</html:link>
                <html:link action="manageKnowledges.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;知识点</html:link>
                <html:link action="manageTemplates.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;试卷模板</html:link>
                <html:link action="gradeManagement.do?action=startMana" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;成绩管理</html:link>
                <html:link page="/teacher/studentMana.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;学生管理</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
   	<!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=tea.getRealname()%>
            </div>
        	<html:link page="/teacher/teacherInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
          	<p><html:link page="/teacher/teaModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
            <p><html:link page="/teacher/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登录</html:link></p>
  		</div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  <div id = "clock">
  			<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            		marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
  		<div id = "main"><center>
            <br /><br />
            	<div id = "subject">
            	<table>
                    <tr>
                        <td width="100" align="right"><span class="text"><strong>科目:</strong></span></td>
                        <td width="130"><select name="course" id="course" style="width:80px;" 
                        onchange="javascript:showKnowledges();">
                                <%=courseOpt %>
                            </select></td>
                    </tr>
                    <tr>
                        <td align="right"><span class="text"><strong>&nbsp;&nbsp;知识点:</strong></span></td>
                        <td><select name="know" id="know" style="width:120px;"
                        onchange="javascript:showQuestions();">
                              <%=knowledgesOpt %>
                            </select></td>
                    </tr>
                
                	<tr>
                    	<td></td>
                    	<td><input type="text" value="输入关键字" name="searchQ" id="searchQ" style="
                    	<%if (session.getAttribute("questions") == null) {%>visibility:hidden;<%} %>"
                    	onfocus="this.value='';"/></td>
                        <td><input type="button" value="搜索" name="searchQBtn" id="searchQBtn" style="width:60px; height:26px;
                        <%if (session.getAttribute("questions") == null) {%>visibility:hidden;<%} %>" 
                        onclick="javascript:searchQuestions();"/></td>
                    </tr>
            	</table>
            	</div>
            <div id = "questions">
            	<table border="1">
                	<tr>
                    	<td height="278" valign="top">
                      		<select name="st_1" size="20" id="st_2" style="width:300px; ">
                        		<%=questionsOpt %>
                      		</select></td>
                	</tr><% if (courses != null && courses.size() != 0){ %>
                	<tr>
                    	<td height="41" align="center">
                        <input type="button" name="addQ" id="addQ" value="添加试题" style="width:80px; height:40px" 
                        onclick="operateQuestions(this.name);" />
                        &nbsp;
                        <input type="button" name="modifyQ" id="modifyQ" value="修改" style="width:80px; height:40px;
                        <%if (session.getAttribute("questions") == null) {%>visibility:hidden;<%} %>" 
                        onclick="operateQuestions(this.name);"/>
                        &nbsp;
                        <input type="button" name="deleteQ" id="deleteQ" value="删除" style="width:80px; height:40px;
                        <%if (session.getAttribute("questions") == null) {%>visibility:hidden;<%} %>" 
                        onclick="operateQuestions(this.name);"/></td>
                	</tr><!-- 
                    <tr>
                    	<td valign="bottom">
                        <form action="manageQuestions.do?action=importQuestions" method="post" onsubmit="return check();">
                        <input type="file" name="import" id="import" style="background-color: #FFF;"/>
                        <input type="submit" value="导入题目"/></form></td>
                    </tr> --><%} %>
            </table>
            </div>
            </center>
   		</div>
	<!-- MAIN END -->
        
	<!-- FOOTER START -->
	<div id = "footer">
  </div>
	<!-- FOOTER END -->
        
</div>
    <!-- CONTAINER END -->
    
</body>
</html>
